package com.dinree.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.base.library.TopBarBaseActivity;
import com.base.library.manager.DialogManager;
import com.dinree.R;
import com.dinree.api.UserApi;
import com.dinree.bean.UserBean;
import com.dinree.databinding.ActivitySettingBinding;
import com.dinree.utils.Constant;
import com.dinree.utils.LaunchUtil;
import com.jakewharton.rxbinding2.view.RxView;
import com.tencent.smtt.sdk.CookieManager;

import java.util.concurrent.TimeUnit;

import static com.base.library.Constant.WINDOW_DURATION;

public class SettingActivity extends TopBarBaseActivity {
    private ActivitySettingBinding activitySettingBinding;
    private UserBean userBean;

    @Override
    public int getContentView() {
        return R.layout.activity_setting;
    }

    @Override
    public void initContentView(Bundle savedInstanceState) {
        activitySettingBinding = getContentViewBinding();
        userBean = realm.findFirst(UserBean.class);
        setTitle("设置");
        setTopLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        RxView.clicks(activitySettingBinding.pwdSetting).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            Intent intent = new Intent(mContext, PwdSettingActivity.class);
            startActivity(intent);
        });
        RxView.clicks(activitySettingBinding.feedback).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            LaunchUtil.launchActivityByUrl(mContext, Constant.WEB_HOST + "front/#/feedback");
        });
        RxView.clicks(activitySettingBinding.law).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            LaunchUtil.launchActivityByUrl(mContext, Constant.WEB_HOST + "front/#/agreement");
        });
        RxView.clicks(activitySettingBinding.clear).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            DialogManager.showYesNoDialog(this, "您确定要清除缓存吗?", "确定", "取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    if (arg1 == DialogInterface.BUTTON_POSITIVE) {
                        showMsg("清理完成");
                    }
                }
            }).show();
        });
        RxView.clicks(activitySettingBinding.versionUp).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
//            DialogManager.showYesNoDialog(this, "检查到有新版本?", "去更新", "取消", new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface arg0, int arg1) {
//                    if (arg1 == DialogInterface.BUTTON_POSITIVE) {
                        showMsg("当前已是最新版本");
//                    }
//                }
//            }).show();
        });
        RxView.clicks(activitySettingBinding.about).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            LaunchUtil.launchActivityByUrl(mContext, Constant.WEB_HOST + "front/#/about");
        });
        RxView.clicks(activitySettingBinding.guide).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            LaunchUtil.launchActivityByUrl(mContext, Constant.WEB_HOST + "front/#/guide");
        });
        RxView.clicks(activitySettingBinding.btnLogout).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            DialogManager.showYesNoDialog(this, "是否确认退出登录?", "确定", "取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    if (arg1 == DialogInterface.BUTTON_POSITIVE) {
                        if (userBean != null) {
                            UserApi.getInstance(mContext).logout(userBean.user_id, userBean.user_token).subscribe(s -> {
                                CookieManager cookieManager = CookieManager.getInstance();
                                cookieManager.removeAllCookie();
                                cookieManager.flush();
                                realm.deleteAll(UserBean.class);
                                Intent intent = new Intent(mContext, LoginActivity.class);
                                startActivity(intent);
                                finish();
                                MainActivity.getInstance().finish();
                            }, e -> {
                                e.printStackTrace();
                            });
                        }
                    }
                }
            }).show();
        });
    }
}

